package com.acme.service.bank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.acme.domain.account.Account;
import com.acme.domain.account.AccountState;
import com.acme.domain.account.DepositComparator;
import com.acme.domain.bank.Bank;
import com.acme.domain.bank.Client;

public class BankReportService {

	public static int getNumberOfClients(Bank bank) {
		return bank.getClientCount();
	}

	public static int getNumberOfOpenAccounts(Bank bank) {
		int i = 0;
		for(Client client : bank.getClientsList()) {
			for (Account account : client.getAccountsList()) {
				if(account.getAccountState().equals(AccountState.OPENED)) i++;
			}
		}
		return i;
	}

	public static int getNumberOfAccounts(Bank bank) {
		int i = 0;
		for(Client client : bank.getClientsList()) {
			i += client.getAccountsList().size();
		}
		return i;
	}

	public static List<Account> getAccountsSortedByDeposit(Bank bank, boolean ascending) {
		List<Account> accounts = new ArrayList<Account>();
		for(Client client : bank.getClientsList()) {
			for(Account account : client.getAccountsList()) {
				accounts.add(account);
			}
		}
		Collections.sort(accounts, new DepositComparator(ascending));
		return accounts;
	}

	public static Map<Client, Account> getCustomerAccounts(Bank bank) {
		Map<Client, Account> map = new HashMap<Client, Account>();
		for(Client client : bank.getClientsList()) {
			for(Account account : client.getAccountsList()) {
				map.put(client, account);
			}
		}
		return map;
	}

	public static double getBankCredit(Bank bank) {
		double result = 0;
		for(Client client : bank.getClientsList()) {
			for(Account account : client.getAccountsList()) {
				if(account.getBalance() < 0) {
					result += account.getBalance();					
				}
			}
		}
		return result;
	}
	
}